package modulesAPI.v2.basics;

import java.awt.BorderLayout;
import java.awt.Component;
import java.util.Collection;
import java.util.LinkedList;

import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;


import com.caterina.components.CLabel;
import com.caterina.components.CPanel;

import modulesAPI.v2.Module;
import modulesAPI.v2.ViewType;

public class ModCollection extends Module<Collection<Module<?>>>implements ListSelectionListener,ListCellRenderer{
	/**
	 * Crea un nuovo modulo per l'inserimento/rappresentazione di una collezione di moduli
	 * */
	public ModCollection(Module<?> ownerModule,Collection <Module<?>> collection,String tag,String description, ViewType viewType,int ...options) {
		super(ownerModule, collection, tag, description, viewType);
		this.refreshList();
		if (options.length>0){
			this.selectedIndex=options[0];
		}
		list.setSelectedIndex(selectedIndex);
		list.addListSelectionListener(this);
		list.setCellRenderer(this);
	}
	public void setSelectedIndex(int index){
		list.setSelectedIndex(index);
		this.selectedIndex=index;
	}
	private void refreshList(){
		list=new JList(this.getContent().toArray());
		if (this.getContent().size()>0)list.setSelectedIndex(0);
	}
	/**Aggiunge un modulo alla lista*/
	public void add(Module<?> mod){
		LinkedList<Module<?>> cnt=new LinkedList<Module<?>>(this.getContent());
		cnt.add(mod);
		this.setContent(cnt);
		this.refreshList();
		this.fireModuleChanged(this);
	}
	/**Rimuove un modulo dalla lista*/
	public void remove(Module<?>mod){
		LinkedList<Module<?>> cnt=new LinkedList<Module<?>>(this.getContent());
		cnt.remove(mod);
		this.setContent(cnt);
		this.refreshList();
		this.fireModuleChanged(this);
	}
	/**Ottiene l'indice del modulo selezionato*/
	public int getSelectedIndex(){
		return this.list.getSelectedIndex();
	}
	/**Ottiene l'istanza del modulo selezionato*/
	public Module<?> getSelectedContent(){
		if (this.list.getSelectedIndex()==-1)return new EmptyModule();
		else return (Module<?>) list.getSelectedValue();
	}
	@Override
	protected CPanel getReadOnlyPanel(boolean isSelected) {
		CPanel retPanel=new CPanel(new BorderLayout());
		retPanel.add(new JScrollPane(this.list));
		return retPanel;
	}
	@Override
	protected CPanel getWritablePanel(boolean isSelected) {
		return this.getReadOnlyPanel(isSelected);
	}
	@Override
	public CPanel getInListPanel(boolean isSelected) { 
		return this.getReadOnlyPanel(isSelected);
	}
	@Override
	public Component getListCellRendererComponent(JList list, Object value,int index, boolean isSelected, boolean cellHasFocus) {
		if (value instanceof Module)
		return ((Module<?>) value).getPanel(ViewType.IN_LIST,isSelected);
		else return new CLabel(value.toString());
	}
	@Override
	public void valueChanged(ListSelectionEvent e) {
		if (this.selectedIndex!=list.getSelectedIndex()){
			this.selectedIndex=list.getSelectedIndex();
			this.fireModuleChanged(this);
		}
	}
	@Override
	protected CPanel getOneLinePanel(boolean isSelected) {
		CPanel panel=new CPanel();
		panel.add(new CLabel(this.getTag()+" ("+this.list.getComponentCount()+")"));
		return panel;
	}
	@Override
	protected void beforeFireChange(Module<?> mod) {}
	@Override
	protected void afterFireChange(Module<?> mod) {}

	private int selectedIndex=-1;

	private JList list;
}
